package com.example.sensor;



import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity
        implements SensorEventListener {
    //定义Sensor管理器
    private SensorManager mSensorManager;
    EditText etAcceleration;
    EditText etDirection;
    EditText etMagnetism;
    EditText etTemperature;
    EditText etLight;
    EditText etPressure;
    Button compass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        compass = (Button)findViewById(R.id.compass);
        compass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,Compass.class);
                startActivity(i);
            }
        });

        etAcceleration = (EditText)findViewById(R.id.et_acceleration);
        etDirection = (EditText)findViewById(R.id.et_direction);
        etMagnetism = (EditText)findViewById(R.id.et_magnetism);
        etTemperature = (EditText)findViewById(R.id.et_temperature);
        etLight = (EditText)findViewById(R.id.et_light);
        etPressure = (EditText)findViewById(R.id.et_pressure);
        //获取传感器管理服务
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    }
    @Override
    protected void onResume(){
        super.onResume();
        //为系统传感器注册监听器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_GAME);
    }
    @Override
    protected void onStop(){
        //程序退出时取消注册传感器监听器
        mSensorManager.unregisterListener(this);
        super.onStop();
    }
    @Override
    protected void onPause(){
        //程序暂停时取消注册传感器监听器
        mSensorManager.unregisterListener(this);
        super.onPause();
    }
    //以下是实现SensorEventListener接口必须实现的方法
    @Override
    //当传感器精度改变时回调该方法
    public void onAccuracyChanged(Sensor sensor,int accuracy){ }
    @Override
    public void onSensorChanged(SensorEvent event){
        float[] values = event.values;
        //获取触发event的传感器类型
        int sensorType = event.sensor.getType();
        StringBuilder sb=null;
        switch (sensorType){
            //加速度传感器
            case Sensor.TYPE_ACCELEROMETER:
                sb = new StringBuilder();
                sb.append("X方向上的加速度：");
                sb.append(values[0]);
                sb.append("\nY方向上的加速度：");
                sb.append(values[1]);
                sb.append("\nZ方向上的加速度：");
                sb.append(values[2]);
                etAcceleration.setText(sb.toString());
                break;
            //方向传感器
            case Sensor.TYPE_ORIENTATION:
                sb = new StringBuilder();
                sb.append("绕Z轴转过的角度：");
                sb.append(values[0]);
                sb.append("\n绕X轴转过的角度：");
                sb.append(values[1]);
                sb.append("\n绕Y轴转过的角度：");
                sb.append(values[2]);
                etDirection.setText(sb.toString());
                break;
            //磁场传感器
            case Sensor.TYPE_MAGNETIC_FIELD:
                sb = new StringBuilder();
                sb.append("X方向上的磁场强度：");
                sb.append(values[0]);
                sb.append("\nY方向上的磁场强度：");
                sb.append(values[1]);
                sb.append("\nZ方向上的磁场强度：");
                sb.append(values[2]);
                etMagnetism.setText(sb.toString());
                break;
            //温度传感器
            case Sensor.TYPE_AMBIENT_TEMPERATURE:
                sb = new StringBuilder();
                sb.append("当前温度为：");
                sb.append(values[0]);
                etTemperature.setText(sb.toString());
                break;
            //光传感器
            case Sensor.TYPE_LIGHT:
                sb = new StringBuilder();
                sb.append("当前光的强度为：");
                sb.append(values[0]);
                etLight.setText(sb.toString());
                break;
            //压力传感器
            case Sensor.TYPE_PRESSURE:
                sb = new StringBuilder();
                sb.append("当前压力为：");
                sb.append(values[0]);
                etPressure.setText(sb.toString());
                break;
        }
    }
}
